package org.traffometer.traffobase.model;

import org.opendmtp.server.base.Packet;
import org.opendmtp.server.db.Payload;

/**
 * Takes a packet and extracts the latitude, longitude, and timestamp values from the payload.
 * Provides access methods for the values.
 * 
 * @author George Lee
 * 
 */
public class PacketParser {

  /**Payload value of the packet.*/
  private String packetPayload;
  
  /**Starting latitude value.*/
  private String startLat;

  /**Starting longitude value.*/
  private String startLong;

  /**Starting timestamp value.*/
  private String startTime;

  /**Ending latitude value.*/
  private String endLat;

  /**Ending longitude value.*/
  private String endLong;

  /**Ending timestamp value.*/
  private String endTime;

  /**
   * Parses a packet and extracts the latitude, longitude, and timestamp values.
   * 
   * @param inPacket The packet to be parsed.
   */
  public PacketParser(Packet inPacket) {
    String payload = "";
    
    if (inPacket.hasPayload()) {
      Payload inPayload = inPacket.getPayload(true);
      payload = inPayload.readString(inPayload.getSize());
    }
    
    //Save the original payload and place it on the stack.
    this.packetPayload = payload;
    //StackModel packetStack = StackModel.getInstance();
    //packetStack.push(payload);
    
    //Parse through the payload and separate out the values.
    String[] values = payload.split("\\s");
    for (String element : values) {
      String[] input = element.split("=");
      
      if (input[0].equals("lat0")) {
        this.startLat = input[1];
      }
      
      else if (input[0].equals("long0")) {
        this.startLong = input[1];
      }
      
      else if (input[0].equals("time0")) {
        this.startTime = input[1];
      }
      
      else if (input[0].equals("lat1")) {
        this.endLat = input[1];
      }
      
      else if (input[0].equals("long1")) {
        this.endLong = input[1];
      }
      
      else if (input[0].equals("time1")) {
        this.endTime = input[1];
      }
    }
    
  }

  /**
   * Get the starting latitude of the packet.
   * 
   * @return A string representation of the starting latitude.
   */
  public String getStartLatitude() {
    return this.startLat;
  }

  /**
   * Get the starting longitude of the packet.
   * 
   * @return A string representation of the ending latitude.
   */
  public String getStartLongitude() {
    return this.startLong;
  }

  /**
   * Get the starting timestamp of the packet.
   * 
   * @return A string representation of the starting timestamp.
   */
  public String getStartTimestamp() {
    return this.startTime;
  }

  /**
   * Get the ending latitude of the packet.
   * 
   * @return A string representation of the ending latitude.
   */
  public String getEndingLatitude() {
    return this.endLat;
  }

  /**
   * Get the ending longitude of the packet.
   * 
   * @return A string representation of the ending latitude.
   */
  public String getEndingLongitude() {
    return this.endLong;
  }

  /**
   * Get the ending timestamp of the packet.
   * 
   * @return A string representation of the ending timestamp.
   */
  public String getEndingTimestamp() {
    return this.endTime;
  }
  
  /**
   * Get a string representation of the payload of the packet.
   * 
   * @return A string representation of the payload.
   */
  public String toString() {
    return this.packetPayload;
  }
}
